import os

from client.func.ClientHttp import is_friend_post, friend_application_post, delete_friend_post, change_remarks_post, \
    get_remarks_post
from client.pages.ChatPage import *
from client.pages.FriendInfoWin import *

root = "./"
img_dir = "client/.cache/img_buf/"


class FriendInfoPage(QMainWindow, Ui_FriendInfoWin):
    def __init__(self):
        super(FriendInfoPage, self).__init__()
        self.setupUi(self)
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.myuid = ""
        self.is_friend = False
        self.uid = ""
        self.sex = "boy"
        self.img_path = "./client/.cache/img_buf/user_default.png"
        self.explain = ""
        self.name = u"昵称"

        self.close_button.clicked.connect(self.OnClose)
        self.chat_button.clicked.connect(self.OnChat)
        self.edit_name_button.clicked.connect(self.OnEditName)
        self.nameEdit.editingFinished.connect(self.OnEditNameFinished)
        self.delete_button.clicked.connect(self.OnDelete)

    def OnDelete(self):
        res = QMessageBox.question(self, u'删除好友', u'是否确定删除此好友？', QMessageBox.Yes | QMessageBox.No)
        if res == QMessageBox.Yes:
            delete_friend_post(self.myuid, self.uid)
            self.update()

    def OnEditName(self):
        self.nameEdit.setReadOnly(False)
        self.nameEdit.selectAll()
        self.nameEdit.setFocus()

    def OnEditNameFinished(self):
        self.nameEdit.setReadOnly(True)
        if self.nameEdit.text():
            self.name = self.nameEdit.text()
            change_remarks_post(self.myuid, self.uid, self.name)  # 修改备注名
        else:
            info = get_user_info_post(self.uid)
            if info:
                self.name = info[1]
                self.nameEdit.setText(self.name)
            else:
                self.nameEdit.setText(self.name)

    def OnChat(self):
        if self.is_friend:
            self.chat_win = ChatPage()  # 聊天框
            self.chat_win.myuid = self.myuid
            self.chat_win.target = self.uid
            self.chat_win.category = "friend"
            self.chat_win.init_update()
            self.chat_win.show()
        else:
            if friend_application_post(self.myuid, self.uid):
                self.chat_button.setText(u"好友申请已发送")

    def calc_relationship(self):
        if self.uid == self.myuid:
            self.edit_name_button.hide()
            self.chat_button.hide()
            self.delete_button.hide()
        else:
            data = is_friend_post(self.myuid, self.uid)
            if data:
                self.is_friend = data[1]
            if self.is_friend:
                data = get_remarks_post(self.myuid, self.uid)
                if data:
                    self.name = data[1]
                self.chat_button.setText(u"聊天")
                self.edit_name_button.show()
                self.chat_button.show()
                self.delete_button.show()
            else:
                self.chat_button.setText(u"加好友")
                self.edit_name_button.hide()
                self.chat_button.show()
                self.delete_button.hide()

    def update(self):
        info = get_user_info_post(self.uid)
        if info:
            self.name = info[1]
            self.img_path = root + img_dir + info[2]
            self.sex = info[3]
            self.explain = info[5]
            self.set_img()
            self.calc_relationship()
            self.nameEdit.setText(self.name)
            self.uid_label.setText("uid: "+self.uid)
            self.sex_label.setText("sex: "+self.sex)
            self.textEdit.setText(self.explain)

    def set_img(self):
        if os.path.exists(self.img_path):
            img = QtGui.QPixmap(self.img_path).scaled(self.img_label.width(), self.img_label.height())
            self.img_label.setPixmap(img)

    def OnClose(self):
        self.close()


